/*
 * KKAdapterLayer.h
 *
 *  Created on: 2013-9-15
 *      Author: wanghua
 */

#ifndef KKADAPTERLAYER_H_
#define KKADAPTERLAYER_H_

#include "kaka_common.h"
#include "KKLayer.h"

USING_NS_CC;

using namespace std;

class KKNodeAdapter: public CCObject {
public:
	virtual int getItemIndex(KKNode* item)=0;
	virtual int getCount() const =0;
	virtual KKNode* getView(int position, KKNode* parent)=0;
};

class KKDefaultAdapter: public KKNodeAdapter {
protected:
CC_SYNTHESIZE(int, m_itemCount,Count)
	;CC_SYNTHESIZE(KKNodeArray*,m_items,Items)
	;
public:

	KKDefaultAdapter();
	virtual ~KKDefaultAdapter();

	virtual void addItem(KKNode* item);
	virtual void removeItem(KKNode* item);
	virtual int getItemIndex(KKNode* item);
	virtual KKNode* getView(int position, KKNode* parent);
};
class KKNodeInfo: public CCObject {
public:
	KKNode* m_node;
public:
	KKNodeInfo(KKNode* node = NULL);
	virtual ~KKNodeInfo();

	inline bool isUsed();
	inline void useNode();
	inline void recycleNode();
};
typedef KKDictionary<int, KKNodeInfo*> KKNodeCache;

class KKNodeRecycler: public CCObject {
private:
	KKNodeCache m_cache;
public:

	KKNodeRecycler(void);
	virtual ~KKNodeRecycler(void);

	bool addToCache(KKNode* node, const int& pos);
	void useNode(KKNode* node);
	bool removeFromCache(const int& pos);
	KKNode* getFromCache(const int& pos);
	void clear();
	void description();
};

class KKAdapterLayer: public KKLayer {
protected:
	KKNodeRecycler m_recycler;
private:
	;CC_SYNTHESIZE_READONLY(int, m_firstPosition,FirstPosition)
	;
	;CC_SYNTHESIZE_READONLY(int, m_lastPosition,LastPosition)
	;
	;CC_SYNTHESIZE_READONLY(KKNode*, m_firstNode,FirstNode)
	;
	;CC_PROPERTY(int,m_selection,Selection)
	;
	;CC_SYNTHESIZE(KKNode*,m_selectedNode,SelectedNode)
	;CC_SYNTHESIZE_READONLY(KKNodeAdapter*,m_adapter,Adapter)
	;
public:
	KKAdapterLayer();
	virtual ~KKAdapterLayer();

	virtual bool initDelayInfoWithXml(TiXmlElement* element);

	virtual void setAdapter(KKNodeAdapter* adapter);

};

#endif /* KKADAPTERLAYER_H_ */

